<?php
/**
 * Created by yujian
 * Date: 2018-10-16
 */

namespace app\admin\controller;

use think\Db;
use think\facade\Request;

class Banner extends MyController
{
    public function index()
    {
        $banner = db('banner')->select();
        $this->assign("banner", $banner);
        return view();
    }

    public function add()
    {
        if ( Request::isPost()) {
            $data = Request::post();

            if (!empty(request()->file('image'))) {
                $data['b_img'] = uploadimg('image', '../public/static/uploads');
            }

            if (db('banner')->insert($data)) {
                $this->redirect('banner/index');
            } else {
                return $this->error('添加失败！');
            }

        }
    }

    public function edit()
    {
        $id = input('id');
        $banner = db('banner')->where('b_id',$id)->find();
        $this->assign("banner", $banner);


        if(request()->isPost()){
            if (!empty(request()->file('image'))) {
                $data['b_img'] = uploadimg('image', '../public/static/uploads');
            }

            if(db('banner')->where('b_id',$id)->update($data)){
                $this->redirect('banner/index');
            }else{
                return $this->error('修改失败！');
            }
        }
        return view();
    }

    public function del(){
        $id  =  input('id');
//        var_dump($id);exit;
        $del = db('banner')->where('b_id',$id)->find();
        $flag = db('banner')->where('b_id',$id)->delete();
        if($flag){
            unlink(config('upload.path').$del['b_img']);
            $this->redirect('banner/index');
        }else{
            $this->error('删除失败！');
        }
    }

}